//
//  XXGK2Net.m
//  上海杨浦
//
//  Created by FrankChen on 13-10-24.
//  Copyright (c) 2013年 FrankChen. All rights reserved.
//

#import "XXGK2Net.h"
#import "ServiceHelper.h"

@implementation XXGK2Net

@synthesize titleArray;
@synthesize linkArray;


-(void)getTile:(NSString *) ModuleId2 :(int)pageIndex{

    
     NSMutableArray *params=[NSMutableArray array];
    //[params addObject:[NSDictionary dictionaryWithObjectsAndKeys:@"0093000200030001",@"ModuleId", nil]];
    //[params addObject:[NSDictionary dictionaryWithObjectsAndKeys:@"20",@"PageSize", nil]];
    //[params addObject:[NSDictionary dictionaryWithObjectsAndKeys:@"1",@"CurrentPageIndex", nil]];
    NSString *page=[NSString stringWithFormat:@"%d",pageIndex];
    
    [params addObject:[NSDictionary dictionaryWithObjectsAndKeys:ModuleId2,@"ModuleId", nil]];
    [params addObject:[NSDictionary dictionaryWithObjectsAndKeys:@"20",@"PageSize", nil]];
    [params addObject:[NSDictionary dictionaryWithObjectsAndKeys:page,@"CurrentPageIndex", nil]];
    
     ServiceArgs *args1=[[[ServiceArgs alloc] init] autorelease];
     args1.serviceURL=@"http://www.shyp.gov.cn/xxgk/Webservice/InfoQuery.asmx";
     args1.serviceNameSpace=@"http://tempuri.org/";
     args1.methodName=@"GetInfoList";
     args1.soapParams=params;
    
    //NSLog(@"soap=%@\n",args1.soapMessage);
     ServiceResult *result=[ServiceHelper syncService:args1];
    
    NSString *xml=[[result.request responseString] stringByReplacingOccurrencesOfString:@"&lt;" withString:@"<"];
    xml=[xml stringByReplacingOccurrencesOfString:@"&gt;" withString:@">"];
    
    //NSLog(@"xml=%@\n",xml);
    NSMutableArray *_linkArray=[[NSMutableArray alloc]init];
    NSMutableArray *_titleArray=[[NSMutableArray alloc]init];
    NSString *tag1=@"<Title>";
    NSString *tag2=@"</Title>";
    NSString *tag3=@"<Infoid>";
    NSString *tag4=@"</Infoid>";
    NSString *title=@"";
    NSString *time=@"";
    NSString *link=@"";
    NSString *tag5=@"<InfoDate>";
    NSString *tag6=@"</InfoDate>";
    BOOL over=false;
    for (int i=0; i<20; i++) {
        NSRange range1=[xml rangeOfString:tag1];
        
        if (range1.length==0) {
                over=true;
        }
        
        if (!over) {
            
            xml=[xml substringFromIndex:(range1.location+range1.length)];
            
            NSRange range2=[xml rangeOfString:tag2];
            title=[xml substringToIndex:range2.location];
            

            NSRange range5=[xml rangeOfString:tag5];
            xml=[xml substringFromIndex:(range5.location+range5.length)];
            NSRange range6=[xml rangeOfString:tag6];
            time=[xml substringToIndex:range6.location];
            NSRange range7=[time rangeOfString:@"0:00:00"];
            time=[time substringToIndex:range7.location];
            
            title=[title stringByAppendingString:@" "];
            title=[title stringByAppendingString:time];
            //title=[title stringByAppendingString:@" 2012-10-10"];
            
            [_titleArray addObject:title];
            
            NSRange range3=[xml rangeOfString:tag3];
            xml=[xml substringFromIndex:(range3.location+range3.length)];
            
            NSRange range4=[xml rangeOfString:tag4];
            link=[xml substringToIndex:range4.location];
            
            [_linkArray addObject:link];
        }
        
    }
    linkArray=_linkArray;
    titleArray=_titleArray;
}

-(NSString *)getData:(NSString *)infoID{
    
    NSMutableArray *params=[NSMutableArray array];

    
    [params addObject:[NSDictionary dictionaryWithObjectsAndKeys:infoID,@"InfoID", nil]];

    
    ServiceArgs *args1=[[[ServiceArgs alloc] init] autorelease];
    args1.serviceURL=@"http://www.shyp.gov.cn/xxgk/Webservice/InfoQuery.asmx";
    args1.serviceNameSpace=@"http://tempuri.org/";
    args1.methodName=@"GetInfoDetail";
    args1.soapParams=params;
    
    //NSLog(@"soap=%@\n",args1.soapMessage);
    ServiceResult *result=[ServiceHelper syncService:args1];
    
    NSString *xml=[[result.request responseString] stringByReplacingOccurrencesOfString:@"&lt;" withString:@"<"];
    xml=[xml stringByReplacingOccurrencesOfString:@"&gt;" withString:@">"];
    
    //NSLog(@"xml=%@\n",xml);
    NSString *tag1=@"<Content>";
    NSString *tag2=@"</Content>";

    NSRange range1=[xml rangeOfString:tag1];
    if (range1.length!=0) {
        xml=[xml substringFromIndex:(range1.location+range1.length)];
        
        NSRange range2=[xml rangeOfString:tag2];
        xml=[xml substringToIndex:range2.location];
    }

    
    
    
    NSString *tag3=[[[NSString alloc]init]autorelease];
    tag3=@"表<SPAN";
    NSString *tag4=[[[NSString alloc]init]autorelease];
    tag4=@"附件：";
    NSString *tag5=[[[NSString alloc]init]autorelease];
    tag5=@"附件</SPAN";
    NSString *tag6=[[[NSString alloc]init]autorelease];
    tag6=@"附件一：";
    NSRange range3=[xml rangeOfString:tag3];
    if (range3.length!=0) {
        xml=[xml substringToIndex:range3.location];
    }
    NSRange range4=[xml rangeOfString:tag4];
    if (range4.length!=0) {
        xml=[xml substringToIndex:range4.location];
    }
    NSRange range5=[xml rangeOfString:tag5];
    if (range5.length!=0) {
        xml=[xml substringToIndex:range5.location];
    }
    NSRange range6=[xml rangeOfString:tag6];
    if (range6.length!=0) {
        xml=[xml substringToIndex:range6.location];
    }
    
    xml=[self filterScript:xml];
    //xml=[self filterHtmlTag:xml];
    //xml=[xml stringByReplacingOccurrencesOfString:@"\"" withString:@"\'"];
    xml=[self removeHTML:xml];
    

    
    xml=[xml stringByReplacingOccurrencesOfString:@"&amp;" withString:@""];
    xml=[xml stringByReplacingOccurrencesOfString:@"nbsp;" withString:@""];
    xml=[xml stringByReplacingOccurrencesOfString:@"ldquo;" withString:@"\""];
    xml=[xml stringByReplacingOccurrencesOfString:@"rdquo;" withString:@"\""];
    xml=[xml stringByReplacingOccurrencesOfString:@"]]>" withString:@""];
    xml=[xml stringByReplacingOccurrencesOfString:@"\n\n\n" withString:@"\n"];
    
    return xml;
}

-(NSString *)getData2:(NSString *)infoID{
    
    NSMutableArray *params=[NSMutableArray array];
    
    
    [params addObject:[NSDictionary dictionaryWithObjectsAndKeys:infoID,@"InfoID", nil]];
    
    
    ServiceArgs *args1=[[[ServiceArgs alloc] init] autorelease];
    args1.serviceURL=@"http://www.shyp.gov.cn/xxgk/Webservice/InfoQuery.asmx";
    args1.serviceNameSpace=@"http://tempuri.org/";
    args1.methodName=@"GetInfoDetail";
    args1.soapParams=params;
    
    //NSLog(@"soap=%@\n",args1.soapMessage);
    ServiceResult *result=[ServiceHelper syncService:args1];
    
    NSString *xml=[[result.request responseString] stringByReplacingOccurrencesOfString:@"&lt;" withString:@"<"];
    xml=[xml stringByReplacingOccurrencesOfString:@"&gt;" withString:@">"];
    
    //NSLog(@"xml=%@\n",xml);
    NSString *tag1=@"<Content>";
    NSString *tag2=@"</Content>";
    NSRange range1=[xml rangeOfString:tag1];
    if (range1.length!=0) {
        xml=[xml substringFromIndex:(range1.location+range1.length)];
        
        NSRange range2=[xml rangeOfString:tag2];
        xml=[xml substringToIndex:range2.location];
        
        
        NSString *tag5=[[[NSString alloc]init]autorelease];
        tag5=@"附表一";
        
        NSRange range5=[xml rangeOfString:tag5];
        if (range5.length!=0) {
            xml=[xml substringToIndex:range5.location];
        }
        
        NSString *tag6=[[[NSString alloc]init]autorelease];
        tag6=@"附件一";
        
        NSRange range6=[xml rangeOfString:tag6];
        if (range6.length!=0) {
            xml=[xml substringToIndex:range6.location];
        }
        
        xml=[self filterScript:xml];
        //xml=[self filterHtmlTag:xml];
        xml=[self removeHTML:xml];
        //NSString *regEx = @"<([^>]*)>";
        //xml = [xml stringByReplacingOccurrencesOfRegex:regEx withString:@""];
        
        xml=[xml stringByReplacingOccurrencesOfString:@"&amp;" withString:@""];
        xml=[xml stringByReplacingOccurrencesOfString:@"nbsp;" withString:@""];
        xml=[xml stringByReplacingOccurrencesOfString:@"ldquo;" withString:@"\""];
        xml=[xml stringByReplacingOccurrencesOfString:@"rdquo;" withString:@"\""];
        xml=[xml stringByReplacingOccurrencesOfString:@"]]>" withString:@""];
        xml=[xml stringByReplacingOccurrencesOfString:@"\n\n\n" withString:@"\n"];
    }
    
    return xml;
}


- (NSString *)removeHTML:(NSString *)html {
    NSScanner *theScanner;
    NSString *text = nil;
    
    theScanner = [NSScanner scannerWithString:html];
    
    while ([theScanner isAtEnd] == NO) {
        // find start of tag
        [theScanner scanUpToString:@"<" intoString:NULL] ;
        
        // find end of tag
        [theScanner scanUpToString:@">" intoString:&text] ;
        
        // replace the found tag with a space
        //(you can filter multi-spaces out later if you wish)
        html = [html stringByReplacingOccurrencesOfString:[NSString stringWithFormat:@"%@>", text] withString:@" "];
        
    }
    return html;
}




- (NSString *)filterScript:(NSString *)originHtmlStr{
    NSString *result = [[[NSString alloc]init]autorelease];
    NSRange arrowTagStartRange = [originHtmlStr rangeOfString:@"<style type=\"text/css\">"];
    if (arrowTagStartRange.location != NSNotFound) { //如果找到
        NSRange arrowTagEndRange = [originHtmlStr rangeOfString:@"</style>"];
        result = [originHtmlStr stringByReplacingCharactersInRange:NSMakeRange(arrowTagStartRange.location, arrowTagEndRange.location - arrowTagStartRange.location+8) withString:@""];
        return [self filterScript:result];
    }
    else{
        result=originHtmlStr;
    }
    return result;
}


- (NSString *)filterHtmlTag:(NSString *)originHtmlStr{
    NSString *htmlresult = [[[NSString alloc]init]autorelease];
    
    NSRange arrowTagStartRange = [originHtmlStr rangeOfString:@"<"];
    if (arrowTagStartRange.location != NSNotFound) { //如果找到
        NSRange arrowTagEndRange = [originHtmlStr rangeOfString:@">"];
        
        htmlresult = [originHtmlStr stringByReplacingCharactersInRange:NSMakeRange(arrowTagStartRange.location, arrowTagEndRange.location - arrowTagStartRange.location + 1) withString:@""];
        
        return [self filterHtmlTag:htmlresult];
        //递归，过滤下一个标签
    }else{
        htmlresult = [originHtmlStr stringByReplacingOccurrencesOfString:@"&nbsp;" withString:@""];  // 过滤&nbsp等标签
        //result = [originHtmlStr stringByReplacingOccurrencesOf  ........
        htmlresult=[htmlresult stringByReplacingOccurrencesOfString:@"\n\n" withString:@"\n"];
        
    }
    
    return htmlresult;
    
}

@end















